﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuDanhGiaNCC2 : UserControl
    {
        public LapPhieuDanhGiaNCC2()
        {
            InitializeComponent();
        }

        private void LapPhieuDanhGiaNCC2_Load(object sender, EventArgs e)
        {
            loadData();
            searchCollapseBtn_Click(null, null);
        }


        private void loadData()
        {
            searchPhieuDanhGia1.refreshGrid();
        }


        private void button5_Click(object sender, EventArgs e)
        {
            // new
            OtherForms.ThemPhieuDanhGiaNCCForm f = new OtherForms.ThemPhieuDanhGiaNCCForm();
            f.ShowDialog();
            loadData();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // edit
            if (dgvPhieuNCC.SelectedRows.Count > 0)
            {
                String maPhieu = (string) dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
                OtherForms.ThemPhieuDanhGiaNCCForm f = new OtherForms.ThemPhieuDanhGiaNCCForm(maPhieu);
                f.ShowDialog();
                loadData();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;

            _DB.PhieuDanhGiaNCC p = (_DB.PhieuDanhGiaNCC)dgvPhieuNCC.SelectedRows[0].Tag;
            if (p.DaDuyet || p.XinDuyet)
                return;

            if (MessageBox.Show("Chắn chắn?", "Xác nhận", 
                                MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            
            // update
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == p.MaPhieu).TrangThai = _DB.TrangThaiPhieu.CHO_DUYET_ID;
            dc.SubmitChanges();
            loadData();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;

            if (MessageBox.Show("Chắn chắn?", "Xác nhận",
                                MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // update
            string maPhieu = (string) dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuDanhGiaNCC p = dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == maPhieu);
            p.deleted = true;
            dc.SubmitChanges();
            loadData();

        }

        private void dgvPhieuNCC_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;

            string maPhieu = (string) dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            OtherForms.XemPhieuDanhGiaNCC f = new OtherForms.XemPhieuDanhGiaNCC(maPhieu);
            f.Show();
        }

        private void searchCollapseBtn_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dgvPhieuNCC.SelectedRows.Count == 0)
                return;
            string maPhieu = (string)dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            //MessageBox.Show(maPhieu);
            Reports.DS_PhieuDanhGiaNCC ds = new Reports.DS_PhieuDanhGiaNCC();
            ds.fillData(maPhieu);
            ds.viewReport();
        }

    }
}
